<!DOCTYPE html>
<html>
<head>
    <script src="static/js/jquery.min.js"></script>
    <script src="static/js/Getparam.js"></script>
    <script src="static/js/data-request.js"></script>
    <meta charset="UTF-8">
    <title>Upload File Page</title>
    <style>
        body {
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }

        h1 {
            text-align: center;
            margin-top: 50px;
        }

        form {
            margin: 50px auto;
            padding: 20px;
            border: 2px solid #007bff;
            border-radius: 10px;
            max-width: 500px;
            background-color: #fff;
        }

        input[type="file"] {
            margin: 10px 0;
            display: none;
        }

        .custom-file-upload {
            border: 2px solid #007bff;
            border-radius: 5px;
            background-color: #fff;
            color: #007bff;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            padding: 10px;
            display: inline-block;
            cursor: pointer;
            font-size: 16px;
            text-align: center;
            vertical-align: middle;
            margin-top: 10px;
        }

        .custom-file-upload span {
            /*margin-left: 10px;*/
            font-weight: bold;
        }

        .file-error {
            color: #f00;
            font-size: 14px;
            margin-top: 10px;
            text-align: right;
        }

        .progress {
            background-color: #ddd;
            height: 20px;
            border-radius: 10px;
            margin-top: 10px;
            overflow: hidden;
            position: relative;
        }

        .progress-bar {
            background-color: #007bff;
            height: 100%;
            width: 0;
            position: absolute;
            top: 0;
            left: 0;
            transition: width 0.3s ease-in-out;
        }

        .progress-value {
            color: #fff;
            font-size: 14px;
            font-weight: bold;
            line-height: 20px;
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
        }

        input[type="submit"] {
            background-color: #007bff;
            color: #fff;
            border: none;
            padding: 10px 20px;
            margin-top: 10px;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        input[type="submit"]:hover {
            background-color: #0062cc;
        }

        .titleContainer {
            /*display: flex;*/
            justify-content: center;
            align-items: center;
            /*margin-bottom: 10px;*/
        }

        .topBar {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }
    </style>
</head>
<body>
<form action="#" method="post" enctype="multipart/form-data" onsubmit="return validateForm()" id="upload-form">
    <!--  点我返回  -->
    <div style="display: flex; justify-content: right">
    <!--    缺乏跳转函数     -->
        <a style="border: none; background-color: white; "href="javascript:return0()" >
            <p style="font-size: 10px; color: gray">返回</p>
        </a>
    </div>
    <!--  标题栏  -->
    <div class="titleContainer">
        <h1 id = "detail-110"></h1>
    </div>
    <!-- 分割线   -->
    <div style="display: flex; justify-content: center">
        <hr style="width: 98%; height: 2px; border: none; background-color: #007bff"/>
    </div>
    <!--  未知  -->
    <div>
        <input type="hidden" name="assignment" id="aid" readonly>
        <input type="hidden" name="course" id="courseName" readonsly>
        <input type="hidden" name="homework" id="assignmentName" readonly>
        <input type="file" id="file-upload" name="file" onchange="showFileName()">
    </div>
    <!--  进度条  -->
    <div class="progress" id="progress">
        <div class="progress-bar" id="progress-bar"></div>
        <div class="progress-value" id="progress-value">0%</div>
    </div>
    <!--  错误操作提示  -->
    <div class="file-error" id="file-error"></div>
    <!--  选择文件上传按钮和上传按钮  -->
    <div class="topBar">
        <div>
            <label for="file-upload" class="custom-file-upload">
                Choose a file to upload
                <span id="file-name"></span>
            </label>
        </div>
        <div>
            <input type="submit" value="Upload" >
        </div>
    </div>
</form>
<script>
    $(function () {
        getData1();
    });

    function getData1() {
        var aid = $.Request("aid");
        var courseName = $.Request("courseName");
        var assignmentName = $.Request("assignmentName");

        console.log(aid);
        console.log(courseName);
        console.log(assignmentName);


        document.getElementById("aid").value = aid;
        document.getElementById("courseName").value = courseName;
        document.getElementById("assignmentName").value = assignmentName;
        $("#detail-110").text(courseName + ": " + assignmentName);


    }

    function showFileName() {
        var fileUpload = document.getElementById("file-upload");
        var fileName = document.getElementById("file-name");

        fileName.innerHTML = fileUpload.value.split("\\").pop();
    }

    function validateForm() {
        var fileUpload = document.getElementById("file-upload");
        var fileError = document.getElementById("file-error");

        // Check if a file has been selected
        if (fileUpload.files.length === 0) {
            fileError.innerHTML = "Please select a file to upload.";
            return false;
        }

        // Check if the file size is within limits (10 MB)
        if (fileUpload.files[0].size > 50 * 1024 * 1024) {
            fileError.innerHTML = "Sorry, this file is too large. Please choose a file that is smaller than 10 MB.";
            return false;
        }

        // Show the progress bar
        var progress = document.getElementById("progress");
        progress.style.display = "block";

        return true;
    }

    // Update the progress bar as the file is being uploaded
    var form = document.getElementById("upload-form");
    form.addEventListener("submit", function (event) {
        event.preventDefault();

        var fileUpload = document.getElementById("file-upload");
        var progressBar = document.getElementById("progress-bar");
        var progressValue = document.getElementById("progress-value");

        var xhr = new XMLHttpRequest();

        xhr.open("POST", "http://localhost:80/data/request/upload");
        xhr.setRequestHeader("token", localStorage.getItem('token'));
        xhr.upload.addEventListener("progress", function (event) {
            var percent = (event.loaded / event.total) * 100;
            progressBar.style.width = percent + "%";
            progressValue.innerHTML = percent.toFixed(0) + "%";
        });
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                alert("File uploaded successfully!");
                form.reset();
                progressBar.style.width = "0";
                progressValue.innerHTML = "0%";
                var fileName = document.getElementById("file-name");
                fileName.innerHTML = "";
                var progress = document.getElementById("progress");
                progress.style.display = "none";
            }
        };
        var formData = new FormData();
        formData.append("assignment", form.assignment.value);
        formData.append("course", form.course.value);
        formData.append("homework", form.homework.value);
        formData.append("file", fileUpload.files[0]);
        xhr.send(formData);
    });
    function return0()
    {
        window.location="assigment.html";
    }
</script>
</body>
</html>